<?php
namespace app\widget\controller;

use tp5_lib\common\controller\BaseController;
use DataWidget\entity\Option;
use DataWidget\widget\FormWidget;

class ConfigController extends BaseController
{
    public function index()
    {
        // $options=Option::fromString();
        $widget=FormWidget::alloc()
        ->setAction(url("update"))
        ->loadFields([
            ["code"=>"text",    "title"=>"文字",    "type"=>"text",     "value"=>"你好", "describe"=>"请输入文字"],
            ["code"=>"number",  "title"=>"数字",    "type"=>"number",   "value"=>50, "describe"=>"请输入数字"],
            ["code"=>"textarea","title"=>"多行文字", "type"=>"textarea", "value"=>"hello world !", "describe"=>"请输入多行文字"],
            ["code"=>"checkbox","title"=>"复选",    "type"=>"checkbox", "value"=>"1", "describe"=>"请选择"],
            ["code"=>"select",  "title"=>"下拉",    "type"=>"select",   "value"=>"1", "describe"=>"请选择","options"=>"0,零;1,壹;2,贰"],
        ]);
        $this->assign("form",$widget->toArray());
        return $this->fetch();
    }
    public function update()
    {
        $options=Option::fromString("0,零;1,壹;2,贰");
        $widget=FormWidget::alloc()
        ->insertFieldArray([
            ["code"=>"text",    "title"=>"文字",    "type"=>"text",     "value"=>"你好", "describe"=>"请输入文字"],
            ["code"=>"number",  "title"=>"数字",    "type"=>"number",   "value"=>50, "describe"=>"请输入数字"],
            ["code"=>"textarea","title"=>"多行文字", "type"=>"textarea", "value"=>"hello world !", "describe"=>"请输入多行文字"],
            ["code"=>"checkbox","title"=>"复选",    "type"=>"checkbox", "value"=>true, "describe"=>"请选择"],
            ["code"=>"select",  "title"=>"下拉",    "type"=>"select",   "value"=>"1", "describe"=>"请选择","options"=>$options],
        ])
        ->input(input());
        $data=[];
        foreach($widget->fields as $field){
            $data[]=$field->archive();
        }
        header('Content-Type: text/plain');
        print_r($data);
        exit;
    }
}
